(ns jna.win
  (:use jna
        jna.win.error))

(def-jna-struct RECT
  [:LONG :left]
  [:LONG :top]
  [:LONG :right]
  [:LONG :bottom])

(defmacro defwin [name function]
  `(defn ~name [& args#]
    (let [code# (apply ~function args#)]
      (if (not= code# ERROR_SUCCESS)
        (throw (RuntimeException. (format "Windows Error: %s returns %s(0x%x)" '~function code# (bit-and 0xFFFFFFFF code#))))
        code#))))

(defn- assert-no-zero [result]
  (if (zero? result)
    (throw (Exception. (str "API returns zero")))
    result))

(defn- assert-is-zero [result]
  (if-not (zero? result)
    (throw (Exception. (str "API returns " result)))
    result))

(defn no-zero [f]
  (comp assert-no-zero f))

(defn is-zero [f]
  (comp assert-is-zero f))


